<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="addPresssionFn(1,0)">添加权限</el-button>
        </div>
        <!-- row-key="id" 才能显示树形数据 -->
        <el-table border :data="list" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{row}">
              <!-- 只有一级才能显示添加功能 -->
              <el-button v-if="row.type===1" type="text" @click="addPresssionFn(2,row.id)">添加</el-button>
              <el-button type="text" @click="editPresssionFn(row.id)">编辑</el-button>
              <el-button type="text" @click="delPresssionFn(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    <!-- 添加/编辑弹框 -->
    <el-dialog :visible="showDialog" :title="fromPression.id?'编辑权限':'新增权限'" @close="closeDialogFn">
      <el-form label-width="100px">
        <el-form-item label="权限名称">
          <el-input v-model="fromPression.name" />
        </el-form-item>
        <el-form-item label="权限标识">
          <el-input v-model="fromPression.code" />
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="fromPression.description" />
        </el-form-item>
        <el-form-item label="权限启用">
          <el-switch
            v-model="fromPression.enVisible"
            active-text="启用"
            active-value="1"
            inactive-text="不启用"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div style="text-align: right;">
          <el-button @click="closeDialogFn">取消</el-button>
          <el-button type="primary" @click="comfireFn">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getPermissionListAPI, addPermissionAPI, delPermissionAPI, getPermissionDetailAPI, updatePermissionAPI } from '@/api/permission'
import { transData } from '@/utils/index'

export default {
  name: 'Permission',
  data() {
    return {
      list: [],
      showDialog: false,
      fromPression: {
        enVisible: '0', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      }

    }
  },
  mounted() {
    this.hGetPermissionList()
  },
  methods: {
    // 获取列表
    async hGetPermissionList() {
      const { data } = await getPermissionListAPI()
      // this.list = data
      // console.log(data)
      this.list = transData(data)
    },
    // 添加按钮
    async addPresssionFn(type, id) {
      this.showDialog = true
      this.fromPression.type = type
      this.fromPression.pid = id
    },
    // 确定按钮
    async  comfireFn() {
      if (this.fromPression.id) {
        await updatePermissionAPI(this.fromPression)
        this.$message.success(`编辑${this.fromPression.type === 1 ? '一级' : '二级'}权限成功`)
      } else {
        await addPermissionAPI(this.fromPression)
        this.$message.success(`添加${this.fromPression.type === 1 ? '一级' : '二级'}权限成功`)
      }

      this.closeDialogFn()
      this.hGetPermissionList()
    },
    // 关闭弹框
    closeDialogFn() {
      this.showDialog = false
      this.fromPression = {
        enVisible: '0', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      }
    },
    // 删除
    async  delPresssionFn(id) {
      // element封装的函数
      this.$confirm('你确认要删除此权限吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 删除操作成功
        await delPermissionAPI(id)
        // 添加提示
        this.$message.success('删除操作成功')
        // 更新数据
        this.hGetPermissionList()
      }).catch(error => {
        console.log(error)
      })
    },
    // 编辑权限
    async  editPresssionFn(id) {
      const res = await getPermissionDetailAPI(id)
      this.fromPression = res.data
      this.showDialog = true
    }
  }
}
</script>
